package com.king.collect;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;

import java.util.Iterator;
import java.util.List;

public class IteratorDemo {
    List<String> list = Lists.newArrayList("Apple","Pear","Banana");
    public static void main(String[] args) {
        IteratorDemo iteratorDemo = new IteratorDemo();
        System.out.println("**********全部过滤************");
        iteratorDemo.allJudege();
        System.out.println("********或得第几个元素********");
        iteratorDemo.getElement();
        System.out.println("******获得符合条件的元素******");
        iteratorDemo.filterElement();
    }
    public void allJudege() {
//new(){}匿名内部类，接口或者抽象类不能被实例化，监听器
        Predicate<String> condition = new Predicate<String>() {
            @Override
            public boolean apply(String input) {
              return input.startsWith("P");
            }
        };

        boolean allIsStartsWithP = Iterators.all(list.iterator(),condition);
        System.out.println(allIsStartsWithP);
    }
    public void getElement(){
        String secondElement = Iterators.get(list.iterator(),1);
        System.out.println("secondElement是："+secondElement);

    }
    public  void filterElement() {

        Iterator<String> startPElements = Iterators.filter(list.iterator(), new Predicate<String>() {
            @Override
            public boolean apply(String input) {
                return input.startsWith("P");
            }
        });
     while (startPElements.hasNext()){
         System.out.println(startPElements.next());

      }
    }
}